package com.wenwan.manage.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wenwan.manage.service.GiveGetService;
import com.wenwan.manage.setting.DefaultValue;

import javax.annotation.Resource;

/**
 * 供求文章
 * 
 * @author Admin
 *
 */
@Controller
@RequestMapping(value = "/give/get")
public class GiveGetController {

	@Autowired
	private GiveGetService giveGetService;

	/**
	 * 发布供求文章
	 * 
	 * @param title
	 *            文章标题
	 * @param content
	 *            文章图文
	 * @param type
	 *            供求类型
	 * @param userId
	 *            发布者ID
	 * @param price
	 *            价格
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	public Map<String, Object> create(@RequestParam(required = true) String title,
			@RequestParam(required = true) String content, @RequestParam(required = true) Boolean type,
			@RequestParam(required = true) String userId, @RequestParam(required = true) Double price) {
		return giveGetService.create(title, content, type, userId, price);
	}

	/**
	 * 删除供求文章
	 * 
	 * @param id
	 *            供求文章ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete/by/id", method = { RequestMethod.POST })
	public Map<String, Object> deleteById(@RequestParam(required = true) String id) {
		return giveGetService.deleteById(id);
	}

	/**
	 * 查询供求文章
	 * 
	 * @param userId
	 *            当前用户的ID
	 * @param currentPage
	 *            当前页
	 * @param pageSize
	 *            每页显示条数
	 * @param isForExamine
	 *            是否查询审核的供求文章
	 * @param isForManager
	 *            是否查询管理的供求文章
	 * @param isForUser
	 *            是否查询其它用户都能看到的文章
	 * @param isExamine
	 *            是否已审核
	 * @param isAdopt
	 *            审核是否审核
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/select/all", method = { RequestMethod.GET })
	public Map<String, Object> selectAll(@RequestParam(required = true) String userId,
			@RequestParam(required = false, defaultValue = DefaultValue.CURRENT_PAGE) Integer currentPage,
			@RequestParam(required = false, defaultValue = DefaultValue.PAGE_SIZE) Integer pageSize,
			@RequestParam(required = false, defaultValue = DefaultValue.FALSE) Boolean isForExamine,
			@RequestParam(required = false, defaultValue = DefaultValue.FALSE) Boolean isForManager,
			@RequestParam(required = false, defaultValue = DefaultValue.FALSE) Boolean isForUser,
			@RequestParam(required = false, defaultValue = DefaultValue.FALSE) Boolean isExamine,
			@RequestParam(required = false, defaultValue = DefaultValue.FALSE) Boolean isAdopt) {
		return giveGetService.selectAll(currentPage, pageSize, userId, isForExamine, isForManager, isForUser, isExamine,
				isAdopt);
	}

	@ResponseBody
	@RequestMapping(value = "/select/one/by/id", method = { RequestMethod.GET })
	public Map<String, Object> selectOneById(@RequestParam(required = true) String id,
			@RequestParam(required = false) String userId) {
		return giveGetService.selectOneById(id, null);
	}

	@ResponseBody
	@RequestMapping(value = "/select/one/by/id/reading", method = { RequestMethod.GET })
	public Map<String, Object> selectOneByIdReading(@RequestParam(required = true) String id,
			@RequestParam(required = true) String userId) {
		return giveGetService.selectOneByIdReading(id, userId);
	}

	@ResponseBody
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	public Map<String, Object> update(@RequestParam(required = true) String id,
			@RequestParam(required = false) String title, @RequestParam(required = false) String content,
			@RequestParam(required = false) Boolean type, @RequestParam(required = false) String userId,
			@RequestParam(required = false) Double price, @RequestParam(required = false) Integer countReading,
			@RequestParam(required = false) Integer countShare, @RequestParam(required = false) Integer countWell,
			@RequestParam(required = false) Boolean isUndercarriage) {
		return giveGetService.update(id, title, content, type, userId, price, null, null, null, isUndercarriage);
	}

	@ResponseBody
	@RequestMapping(value = "/select/recommend", method = { RequestMethod.POST })
	public Map<String, Object> selectRecommend(
			@RequestParam(required = false, defaultValue = DefaultValue.CURRENT_PAGE) Integer currentPage,
			@RequestParam(required = false, defaultValue = "2") Integer pageSize) {
		return giveGetService.selectRecommend(currentPage, pageSize);
	}

}
